// pages/orderdetail/orderdetail.js
var util = require("../../utils/util.js");
const app = getApp();
var state = '';
var baytype = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopList:{},
    orderId: '',
    orderstate: '未付款',
    sendstate: '到店自提',
    name: '姓名',
    phone: '电话',
    address: '天津市西青区杨柳青镇新华道220号正和宜公司',
    time: '2018-0-0',
    timeState: true,
    submit: '付款',
    couponname: '未使用优惠券',
    message: '无',
    downTimeStr:'',
    downTime:1,
    sum: '',
    isHidenDownTime:true,      //是否隐藏倒计时
    isComplete:false,            //是否已经交易完成


    getName:'收货人',
    getAddress:'收货地址'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    // console.log(options.orId)
    self.setData({
      orderId:options.orId
    })
    // console.log(self.data.orderId)
   
  },


  ordersubmit: function () {
    
    var self = this;
    if (state == '付款') {
      if (self.data.downTime <= 0) {
        return;
      }
      var url = app.serverUrl + 'About/orderby';
      wx.request({
        url: url,
        method: 'post',
        data: {
          openid: app.openid,
          orderid: self.data.orderId
        },
        success: function (res) {

          // console.log(res)
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            success: function (res) {
              wx.showToast({
                title: '支付成功',
              })
              state = '待发货'
              self.setData({
                submit: state
              })
            },
            fail: function (res) {
              wx.showToast({
                image: '/image/icon_ca.png',
                title: '支付失败',
              })
            }
          })
        }
      })
    } else if (state == '确认收货') {
      wx.showModal({
        title: '是否确认？',
        content: '确认收货',
        success: function (res) {
          if (res.confirm) {
            var murl = app.serverUrl + 'About/qrOrder';
            wx.request({
              url: murl,
              method: 'post',
              data: {
                orderid: self.data.orderId
              },
              success: function (res) {
                wx.navigateBack(1)
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
        
      })
    }
  },

  // 退款,查看物流，审核中
  mRefund:function(){
    var self = this;
    if (self.data.isComplete=='退款'){
      wx.navigateTo({
        url: '/pages/refund/refund?orderid=' + this.data.orderId,
      })
    } else if (self.data.isComplete == '审核中'){
      wx.showToast({
        icon:'none',
        title: '退款正在审核中...',
      })
    } else if (self.data.isComplete == '查看物流'){
      wx.navigateTo({
        url: '/pages/wuliu/wuliu?orderid=' + self.data.orderId,
      })
    }
    
    
  },
 
  // 跳转详情
  toDatail: function (e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.goodsid;
    // console.log(id);
    wx.navigateTo({                                   //保留本页面，跳转到另一个页面
      url: '/pages/fpdetail/fpdetail?goodsid=' + id,       //传url+值
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var self = this;
    console.log(self);

    var myTime = setInterval(function () {
      self.setData({
        downTime:self.data.downTime-1
        
      })
      var minute = parseInt(self.data.downTime / 60);
      var second = parseInt(self.data.downTime % 60);

      if (self.data.downTime <= 0) {
        self.setData({
          downTimeStr: '00分00秒',
          submit: '已超时'
        })
        clearInterval(myTime)

      } else {
        self.setData({
          downTimeStr: minute + '分' + second + '秒'
        })
      }
    }, 1000);
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var self=this;
    wx.showLoading({
      title: '正在加载...',
    })
    var url = app.serverUrl + 'About/orderGoodsDetail';
    wx.request({
      url: url,
      method: 'post',
      data: {
        orderid: self.data.orderId
      },
      success: function (res) {
        console.log('订单详情', res)

        if (res.data.data.order.orderstate == '1') {
          state = '付款'
          baytype = res.data.data.order.orderstate
          self.setData({
            isHidenDownTime: false

          })
        } else if (res.data.data.order.orderstate == '2') {
          baytype = res.data.data.order.orderstate
          state = '待发货'
        } else if (res.data.data.order.orderstate == '3') {
          baytype = res.data.data.order.orderstate
          state = '确认收货'
        } else if (res.data.data.order.orderstate == '4') {
          baytype = res.data.data.order.orderstate
          state = '已完成'
        }
        var sendstate = '';
        if (res.data.data.order.ordertype == '1') {
          self.setData({
            sendstate: '快速发货',
            timeState: true,
            address: res.data.data.order.orderaddress,
            getName: '收货人',
            getAddress: '收货地址'
          })
        } else {
          self.setData({
            sendstate: '到店自提',
            timeState: false,
            address: app.address,
            getName: '收货人',
            getAddress: '收货地址'
          })
        }
        if (res.data.data.order.liuyan != '') {
          self.setData({
            message: res.data.data.order.liuyan
          })
        }
        self.setData({
          shopList: res.data.data.detail,
          submit: state,
          name: res.data.data.order.username,
          phone: res.data.data.order.tel,
          sum: res.data.data.detail.goodsmoney,
          downTime: Number(res.data.data.order.end),
          // isComplete: res.data.data.tuikuan
        })
        console.log(self.data.downTime);
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: '/pages/index/index'
    }
  
  }
})